package com.heima.article.listen;

import com.heima.article.service.ApArticleService;
import com.heima.common.constants.message.PublishArticleConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
@Slf4j
public class PublishArticleListener {
    //依赖注入
    @Autowired
    private ApArticleService apArticleService;
    /**
     * queues = "队列名称"    当队列存在时，会监听指定队列，队列不存在会报错，不会自动创建
     * queuesToDeclare        当队列存在时，会监听指定队列,队列不存在会自动创建
     * bindings    当需要创建exchange  queue binding时，使用bindings来进行创建
     */
    @RabbitListener(queuesToDeclare =@Queue(PublishArticleConstants.PUBLISH_ARTICLE_QUEUE))
    public void handleAutoScanMsg(String newsId){
        log.info("成功接收到  定时发布文章消息  消息内容:{}  触发时间:{}",newsId, LocalDateTime.now().toString());
        apArticleService.publishArticle(Integer.valueOf(newsId));
    }
}
